/**
 * 数据路由
 */

const Router = require('koa-router')
const router = new Router()
router.prefix('/api/book')
const { HotBook } = require('@model/hot-book.js')
const { SuccessModel } = require('@model/resModel.js')
const { Book } = require('@model/book.js')
const { BookDateValidator } = require('@validator/validator')
const { BookComment } = require('@model/book-comment.js')

router.get('/hot_list', async (ctx, next) => {
  const bookList = await HotBook.getList()
  ctx.body = new SuccessModel(bookList, ctx.requestApi)
})

router.get('/detail/:date', async (ctx, next) => {
  const v = await new BookDateValidator().validate(ctx)
  const date = v.get('path.date')
  const book = await Book.getDetail(date)
  ctx.body = book
})

router.post('/add_comment/:date', async (ctx, next) => {
  const v = await new BookDateValidator().validate(ctx)
  const bookId = v.get('path.date')
  const content = v.get('body.content')
  await BookComment.addComment(bookId, content)
  ctx.body = new SuccessModel({
    msg: '评论成功'
  }, ctx.requestApi)
})

router.get('/comment_list/:date', async (ctx, next) => {
  const v = await new BookDateValidator().validate(ctx)
  const bookId = v.get('path.date')
  const commentList = await BookComment.getCommentList(bookId)
  ctx.body = new SuccessModel(commentList, ctx.requestApi)
})

module.exports = router